﻿/***************
vs1.1框架基础机制
***************/
//#region 命名空间,$vs11,vs11,dom = $vs11.dom,vs11.dom
if (window.vs11 == null) var $vs11 = window.$vs11 = {}, vs11 = window.vs11 = {};
dom = $vs11.dom = {}; vs11.dom = {} 
//#endregion
//#region 基础设计
$vs11.designMode = $vs11.debugMode = $vs11.testMode = false;
vs11.DesignModeClass = function () { $vs11.designMode = (this.constructor != vs11.DesignModeClass) }; new vs11.DesignModeClass();
$vs11.copyToObject = $vs11.sameObject = function (sourceObject, targetObject) { for (p in sourceObject) { targetObject[p] = sourceObject[p]; } }
$vs11.copyObject = function (sourceObject) { var target = {}; for (p in sourceObject) { target[p] = sourceObject[p]; } return target; }
var vs11class = window.classvs11 = function (baseType, newType) { $vs11.sameObject(baseType, newType); }
//#endregion
//#region 语法改善
$vs11.foreach = function (arrInput, itemHandler) {
	/// <summary>itemHandler执行后,返回值为false时,break</summary>
	for (var j = 0, lenJ = arrInput.length; j < lenJ; j++) { if (itemHandler(arrInput[j]) === false) break; }
}
$vs11.foreachSafe = function (arrInput, itemHandler) {
	/// <summary>返回值为false时,break</summary>
	arr = [];
	if (arrInput.constructor == Array) { arr = [].concat(arrInput); } else
	{ for (var i = 0, len = arrInput.length; i < len; i++) { arr.push(arrInput[i]); } }
	for (var j = 0, lenJ = arr.length; j < lenJ; j++) { if (itemHandler(arr[j]) === false) break; }
}
//#endregion
//#region 对非DOM节点的数组形式的事件扩展,支持跨iframe,跨window
$vs11.addEvent = function (sender, ev) {
	var handlers = [];
	handlers.safePush = function (handler) {
		for (var i = 0, len = this.length; i < len; i++) {
			if (this[i] == handler) return false;
		}
		this.push(handler);
		return true;
	}
	var endHandlers = [];
	endHandlers.safePush = function (handler) {
		for (var i = 0, len = this.length; i < len; i++) {
			if (this[i] == handler) return false;
		}
		this.push(handler);
		return true;
	}
	if (sender["On" + ev]) return; sender["On" + ev] = handlers; sender["On" + ev + "_End"] = endHandlers;
	sender[ev] = function () {
		var evs = sender["On" + ev]; var goOn;
		for (var i = 0, evCount = evs.length; i < evCount; ) {
			var isLinkAvoid = false;
			try { var a = evs[i].constructor; } catch (e) { isLinkAvoid = true; }
			if (isLinkAvoid || typeof (evs[i]) == "undefined") {
				evs.splice(i, 1); evCount--; continue;
			}
			goOn = evs[i].apply(sender, arguments);
			if (goOn === false) { sender[ev + "_End"].apply(sender, arguments); return false; }
			i++;
		}
		sender[ev + "_End"].apply(sender, arguments);
		return true;
	}
	sender[ev + "_End"] = function () {
		var evs = sender["On" + ev + "_End"]; var goOn;
		for (var i = 0, evCount = evs.length; i < evCount; ) {
			if (Object.isLinkAvoid(evs[i]) || typeof (evs[i]) == "undefined") {
				evs.splice(i, 1); evCount--; continue;
			}
			goOn = evs[i].apply(sender, arguments);
			if (goOn === false) return false;
			i++;
		}
		return true;
	}
}
//#endregion
//#region Dom事件机制的跨浏览器平台化
$vs11.getWindow = function (docElement) {
	var doc = docElement.ownerDocument || docElement.document || docElement;
	return doc.defaultView || doc.parentWindow;
}
$vs11.addHandler = function (element, eventName, handler) {
	if (!element._events) element._events = {};
	var eventCache = element._events[eventName];
	if (!eventCache) {
		element._events[eventName] = eventCache = [];
	}
	var browserHandler;
	if (element.attachEvent) {
		browserHandler = function () {
			var e = {};
			try { e = $vs11.getWindow(this).event; } catch (ex) { }
			return handler.call(element, e);
		}
		element.attachEvent("on" + eventName, browserHandler);
	} else if (element.addEventListener) {
		browserHandler = function (e) {
			return handler.call(element, e);
		}
		element.addEventListener(eventName, browserHandler, false);
	}
	eventCache[eventCache.length] = { handler: handler, browserHandler: browserHandler }
}
$vs11.removeHandler = function (element, eventName, handler) {
	var cache = element._events[eventName];
	var browserHandler;
	if (!(cache instanceof Array)) throw new Error("No Handler created by $vs11.addHandler");
	for (var i = 0, l = cache.length; i < l; i++) {
		if (cache[i].handler === handler) {
			browserHandler = cache[i].browserHandler;
			break;
		}
	}
	if (element.removeEventListener) {
		element.removeEventListener(eventName, browserHandler, false);
	}
	else if (element.detachEvent) {
		element.detachEvent('on' + eventName, browserHandler);
	}
	cache.splice(i, 1);
}
//#endregion
//#region 扩展可换行文本语法$vs.FS(function(){})
$vs11.FString = function (f) {
	var s = f.toString();
	var start = s.indexOf("*") + 1, len = s.lastIndexOf("*") - start;
	return s.substr(start, len);
}
//#endregion


